package com.alipay.app.groupmeal.standard.ui.b.function;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alipay.app.groupmeal.standard.R;
import com.alipay.app.groupmeal.standard.http.bean.resp.AliOrderDTO;
import com.alipay.app.groupmeal.standard.http.bean.resp.Result;
import com.alipay.app.groupmeal.standard.offline.bean.OrderBean;
import com.alipay.app.groupmeal.standard.offline.manager.OrderDataManager;
import com.alipay.app.groupmeal.standard.ui.MainActivity;
import com.alipay.app.groupmeal.standard.ui.b.util.WIth;
import com.alipay.app.groupmeal.standard.widget.business.BScreenRecyclerView;
import com.alipay.app.groupmeal.standard.widget.business.BScreenTitleView;
import com.alipay.app.groupmeal.standard.widget.business.BaseBScreenView;
import com.alipay.app.groupmeal.standard.widget.business.BottomSnView;
import com.alipay.sdk.util.TextUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.internal.observers.BlockingBaseObserver;

/**
 * @ProjectName: 团餐
 * @ClassName: BScreenSettingList
 * @Description: B屏交易订单列表
 * @Author: bd-001935
 * @CreateDate: 4/14/22 4:08 PM
 * @UpdateUser: bd-001935
 * @UpdateDate: 4/14/22 4:08 PM
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class BScreenOrderList extends BaseBScreenView {

    private BScreenTitleView mBsTitleView;
    private BScreenRecyclerView mBsRecyclerView;
    private BottomSnView mBsSnView;
    private List<AliOrderDTO> mList = new ArrayList<>();
    private BaseQuickAdapter<AliOrderDTO, BaseViewHolder> mAdapter;
    private View mEmptyView;
    private LinearLayoutManager mLayoutManager;

    public BScreenOrderList(Context context) {
        this(context, null);
    }

    public BScreenOrderList(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BScreenOrderList(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_list;
    }

    @Override
    protected void initView(View view) {
        mBsTitleView = view.findViewById(R.id.bsTitleView);
        mBsRecyclerView = view.findViewById(R.id.bsRecyclerView);
        mBsSnView = view.findViewById(R.id.bsSnView);
        mEmptyView = view.findViewById(R.id.emptyView);
    }

    @Override
    protected void initData() {
        mBsTitleView.setVisibility(GONE);
        mBsSnView.setVisibility(GONE);
        initAdapter();
    }

    private void initAdapter() {
        mAdapter = new BaseQuickAdapter<AliOrderDTO, BaseViewHolder>(R.layout.item_bill, mList) {
            @Override
            protected void convert(@NonNull BaseViewHolder baseViewHolder, AliOrderDTO bean) {
                LinearLayout root = baseViewHolder.findView(R.id.ll_bill_root);
                baseViewHolder.setText(R.id.tv_amount, TextUtil.formatPrice(bean.getAmount()));
                String status;
                int color;
                if (bean.isHavePost()) {
                    status = "未同步";
                    color = R.color.txt_333333;
                } else {
                    status = "交易成功";
                    color = R.color.txt_333333;
                }
//                SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日HH:mm");
                baseViewHolder.setText(R.id.tv_name, bean.getUserName());
                baseViewHolder.setText(R.id.tv_pay_state, status);
                baseViewHolder.setText(R.id.tv_date, bean.getCreateTime());
                baseViewHolder.setTextColor(R.id.tv_pay_state, getResources().getColor(color));
            }
        };

        mBsRecyclerView.setAdapter(mAdapter);
        mLayoutManager = new LinearLayoutManager(getContext(),
                LinearLayoutManager.VERTICAL, false);
        mBsRecyclerView.setLayoutManager(mLayoutManager);
    }

    public void setList() {

        new WIth(MainActivity.mActivity).getOder(new BlockingBaseObserver<Result<List<AliOrderDTO>>>() {
            @Override
            public void onNext(@io.reactivex.rxjava3.annotations.NonNull Result<List<AliOrderDTO>> listResult) {
                List<AliOrderDTO> list = listResult.getResult();
                mAdapter.setList(new ArrayList<>());
                if (list != null && !list.isEmpty()) {
                    mAdapter.setList(list);
                } else {
                    mAdapter.setList(new ArrayList<>());
                }
                if (mAdapter.getItemCount() > 0) {
                    mEmptyView.setVisibility(GONE);
                    mBsRecyclerView.setVisibility(VISIBLE);

                } else {
                    mEmptyView.setVisibility(VISIBLE);
                    mBsRecyclerView.setVisibility(GONE);
                }
                show();
            }

            @Override
            public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {

            }
        });


    }

    public void setNoHavePostList() {
        List<OrderBean> orderBeanList = OrderDataManager.getInstance().queryNoHavePostLim();
        List<AliOrderDTO> list = new ArrayList<>();
        for (OrderBean goo : orderBeanList) {
            AliOrderDTO aliOrderDTO = new AliOrderDTO();
            aliOrderDTO.setHavePost(true);
            aliOrderDTO.setUserName(goo.getUserName());
            aliOrderDTO.setAmount(goo.getAmount());
            aliOrderDTO.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goo.getCreateTime()));
            list.add(aliOrderDTO);
        }
        if (list != null && !list.isEmpty()) {
            mAdapter.setList(list);
        } else {
            mAdapter.setList(new ArrayList<>());
        }

        if (mAdapter.getItemCount() > 0) {
            mEmptyView.setVisibility(GONE);
            mBsRecyclerView.setVisibility(VISIBLE);
        } else {
            mEmptyView.setVisibility(VISIBLE);
            mBsRecyclerView.setVisibility(GONE);
        }
        show();
    }
}
